package ua.amsoft.brelok;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

import ua.amsoft.brelok.chart.BrelokXYChartFactory;
import ua.amsoft.brelok.chart.DefaultBarChartCreator;
import ua.amsoft.brelok.dto.ChartRendererParameters;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class AssignmentsFragment extends Fragment {
    
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_assignments, container, false);
        return rootView;
    }
    
    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final BrelokXYChartFactory chartFactory = new DefaultBarChartCreator();
        
        final BarChart chart = chartFactory.createChartToDraw(this.buildRendererValues(),
                this.getChartParams()).draw();
        
        final ImageView chartImageView = (ImageView) this.getActivity().findViewById(
                R.id.bar_chart_top);
        final View v = new GraphicalView(this.getActivity().getApplicationContext(), chart);
        v.layout(0, 0, 400, 250);
        v.setDrawingCacheEnabled(true);
        chartImageView.setImageBitmap(v.getDrawingCache());
        
        final BarChart chart2 = chartFactory.createChartToDraw(this.buildRendererValues(),
                this.getChart2Params()).draw();
        final ImageView chartImageViewDown = (ImageView) this.getActivity().findViewById(
                R.id.bar_chart_bottom);
        final View v2 = new GraphicalView(this.getActivity().getApplicationContext(), chart2);
        v2.layout(0, 0, 400, 250);
        v2.setDrawingCacheEnabled(true);
        chartImageViewDown.setImageBitmap(v2.getDrawingCache());
    }
    
    private XYMultipleSeriesRenderer buildRendererValues() {
        final XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setXLabels(0);
        renderer.setYLabels(10);
        renderer.setXLabelsAlign(Align.LEFT);
        renderer.setYLabelsAlign(Align.LEFT);
        renderer.setPanEnabled(true, false);
        renderer.setMarginsColor(this.getResources().getColor(R.color.a_dkgray));
        renderer.setZoomRate(1.1f);
        renderer.setBarSpacing(0.5f);
        renderer.setShowGridX(true);
        this.setChartSettings(renderer, 0.5, 4.5, 0, 105, Color.GRAY, Color.LTGRAY);
        return renderer;
    }
    
    private void setChartSettings(final XYMultipleSeriesRenderer renderer, final double xMin,
            final double xMax, final double yMin, final double yMax, final int axesColor,
            final int labelsColor) {
        final Context context = this.getActivity().getApplicationContext();
        renderer.setChartTitle(context.getString(R.string.chart_controlled_in_assignment));
        renderer.setXTitle(context.getString(R.string.chart_x_title));
        renderer.setYTitle("");
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }
    
    private ChartRendererParameters getChartParams() {
        final ChartRendererParameters chartParameters = new ChartRendererParameters();
        final Context context = this.getActivity().getApplicationContext();
        final Map<String[], List<double[]>> values = new HashMap<String[], List<double[]>>();
        final List<double[]> chartColumnsValues = new ArrayList<double[]>();
        
        final int[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
        final String[] valuesNames = { context.getString(R.string.red_value),
                context.getString(R.string.yellow_value),
                context.getString(R.string.green_value) };
        
        final double[] percents = { 12, 45, 43, 76 };
        final double[] percents1 = { 15, 85, 93, 43 };
        final double[] percents2 = { 82, 95, 63, 12 };
        
        chartColumnsValues.add(percents);
        chartColumnsValues.add(percents1);
        chartColumnsValues.add(percents2);
        values.put(valuesNames, chartColumnsValues);
        
        chartParameters.setChartValues(values);
        chartParameters.setColors(colors);
        return chartParameters;
    }
    
    private ChartRendererParameters getChart2Params() {
        final ChartRendererParameters chartParameters = new ChartRendererParameters();
        final Context context = this.getActivity().getApplicationContext();
        final Map<String[], List<double[]>> values = new HashMap<String[], List<double[]>>();
        final List<double[]> chartColumnsValues = new ArrayList<double[]>();
        
        final int[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
        final String[] valuesNames = { context.getString(R.string.red_value),
                context.getString(R.string.yellow_value),
                context.getString(R.string.green_value) };
        
        final double[] percents = { 62, 55, 43, 46 };
        final double[] percents1 = { 15, 58, 93, 53 };
        final double[] percents2 = { 23, 95, 76, 36 };
        
        chartColumnsValues.add(percents);
        chartColumnsValues.add(percents1);
        chartColumnsValues.add(percents2);
        values.put(valuesNames, chartColumnsValues);
        
        chartParameters.setChartValues(values);
        chartParameters.setColors(colors);
        return chartParameters;
    }
}
